package com.menhai.personmanagement.services;

import com.menhai.personmanagement.entity.Frontpage;
import com.menhai.personmanagement.entity.SysNav;
import com.menhai.personmanagement.entity.SysRole;
import com.menhai.personmanagement.entity.vo.SysNavVo;
import com.menhai.personmanagement.mapper.FrontpageMapper;
import com.menhai.personmanagement.mapper.SysNavMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysNavService {
    @Autowired
    private SysNavMapper sysNavMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private FrontpageMapper frontpageMapper;

//    public List getNav(String level, int id) {
//        List<SysNav> list = sysNavMapper.getNavByLevelnum(level, "1");
//        List<SysNav> list1 = sysNavMapper.getNavByLevelnum(level, "2");
//        List<SysNavVo> l = new ArrayList<>();
//        Map map = new HashMap();
//        for (int i = 0; i < list.size(); i++) {
//            SysNavVo sysNavVo = new SysNavVo();
//            sysNavVo.setNavId(list.get(i).getNavId());
//            sysNavVo.setIco(list.get(i).getIco());
//            sysNavVo.setOrder(list.get(i).getOrder());
//            sysNavVo.setParentid(list.get(i).getParentid());
//            sysNavVo.setText(list.get(i).getText());
//            sysNavVo.setUrl(list.get(i).getUrl());
//            sysNavVo.setLevel(list.get(i).getLevel());
//            sysNavVo.setLevelnum(list.get(i).getLevelnum());
//            sysNavVo.setMore(list.get(i).getMore());
//            List<SysNav> children = new ArrayList<>();
//            for (int j = 0; j < list1.size(); j++) {
//                if (list.get(i).getNavId() == Integer.parseInt(list1.get(j).getParentid())) {
//                    children.add(list1.get(j));
//                }
//            }
//            sysNavVo.setChildren(children);
//            l.add(sysNavVo);
//        }
//        getUserNavId(id);
//        return l;
//    }
    public List getNavList(int id){
        List<SysNavVo> returnList = new ArrayList<>();
        String[] nid = getUserNavId(id);
        String nids = "";
        for (String s : nid) {
            nids+="'" + s + "',";
        }
        if(StringUtils.isNotEmpty(nids)){
            nids = nids.substring(0,nids.length()-1);
        }
        List<SysNav> navListL1 = sysNavMapper.getNavListByNavids(nids,"1");
        List<SysNav> navListL2 = sysNavMapper.getNavListByNavids(nids,"2");
        for (SysNav sysNav : navListL1) {
            SysNavVo sysNavVo = new SysNavVo();
            sysNavVo.setNavId(sysNav.getNavId());
            sysNavVo.setIco(sysNav.getIco());
            sysNavVo.setOrder(sysNav.getOrder());
            sysNavVo.setParentid(sysNav.getParentid());
            sysNavVo.setText(sysNav.getText());
            sysNavVo.setUrl(sysNav.getUrl());
            sysNavVo.setLevel(sysNav.getLevel());
            sysNavVo.setLevelnum(sysNav.getLevelnum());
            sysNavVo.setMore(sysNav.getMore());
            sysNavVo.setAbbreviation(sysNav.getAbbreviation());
            List<SysNav> children = new ArrayList<>();
            for (SysNav sysNav2 : navListL2) {
//                System.out.println(sysNav2.getParentid() +"======"+sysNav.getNavId());
                if(sysNav2.getParentid().equals(sysNav.getNavId() + "")){
                    children.add(sysNav2);
                }
            }
            sysNavVo.setChildren(children);
            returnList.add(sysNavVo);
        }
        return returnList;
    }
    public List getNav(String level, int id) {
        List<SysNav> list = sysNavMapper.getNavByLevelnum(level, "1");
        List<SysNav> list1 = sysNavMapper.getNavByLevelnumTwo(level, "2");
        List<SysNavVo> l = new ArrayList<>();
        Map map = new HashMap();
        String[] nid = getUserNavId(id);
        for (int i = 0; i < list.size(); i++) {
            SysNavVo sysNavVo = new SysNavVo();
            sysNavVo.setNavId(list.get(i).getNavId());
            sysNavVo.setIco(list.get(i).getIco());
            sysNavVo.setOrder(list.get(i).getOrder());
            sysNavVo.setParentid(list.get(i).getParentid());
            sysNavVo.setText(list.get(i).getText());
            sysNavVo.setUrl(list.get(i).getUrl());
            sysNavVo.setLevel(list.get(i).getLevel());
            sysNavVo.setLevelnum(list.get(i).getLevelnum());
            sysNavVo.setMore(list.get(i).getMore());
            List<SysNav> children = new ArrayList<>();
            for (int j = 0; j < list1.size(); j++) {
                boolean flag = false;
                for(int x = 0;x < nid.length; x++){
                    if(Integer.parseInt(nid[x]) == list1.get(j).getNavId()){
                        flag = true;
                    }
                    if(flag){
                        break;
                    }
                    if (flag) {
                        children.add(list1.get(j));
                        flag = false;
                    }
                }
                if (flag) {
                    if (list.get(i).getNavId() == Integer.parseInt(list1.get(j).getParentid())) {
                        children.add(list1.get(j));
                        flag = false;
                    }
                }
            }
            sysNavVo.setChildren(children);
            l.add(sysNavVo);
        }
        return l;
    }


    public String[] getUserNavId(int id) {
        List<SysRole> list2 = sysUserService.findUserById(id);
        String temp = list2.get(0).getPowers();
        String[] r = temp.split("\\|");
        return r;
    }

    public List selectAll() {
        List<SysNav> list = sysNavMapper.getNavOne(1);
        List<SysNav> list1 = sysNavMapper.getAllLevelTwo();
        List<SysNavVo> l = new ArrayList<>();
        Map map = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            SysNavVo sysNavVo = new SysNavVo();
            sysNavVo.setNavId(list.get(i).getNavId());
            sysNavVo.setIco(list.get(i).getIco());
            sysNavVo.setOrder(list.get(i).getOrder());
            sysNavVo.setParentid(list.get(i).getParentid());
            sysNavVo.setText(list.get(i).getText());
            sysNavVo.setUrl(list.get(i).getUrl());
            sysNavVo.setLevel(list.get(i).getLevel());
            sysNavVo.setLevelnum(list.get(i).getLevelnum());
            sysNavVo.setMore(list.get(i).getMore());
            List<SysNav> children = new ArrayList<>();
            for (int j = 0; j < list1.size(); j++) {
                String val1 = list.get(i).getLevel();
                String val2 = list1.get(j).getLevel();
                if(val1.equals(val2)){
                    children.add(list1.get(j));
                }
            }
            sysNavVo.setChildren(children);
            l.add(sysNavVo);
        }
        List temp =formatData(l);
        return temp;
    }

    public List formatData(List<SysNavVo> list) {
        List t = new ArrayList();
        for(int i=0;i<list.size();i++){
            Map map = new HashMap();
            map.put("id",list.get(i).getNavId());
            map.put("label", list.get(i).getText());
            List temp = new ArrayList();
            for(int j=0;j<list.get(i).getChildren().size();j++){
                Map children = new HashMap();
                children.put("id",list.get(i).getChildren().get(j).getNavId());
                children.put("label",list.get(i).getChildren().get(j).getText());
                temp.add(children);
            }
            map.put("children", temp);
            t.add(map);
        }
        return t;
    }
}

